package com.mobile.web.workOrder;

import com.mobile.service.dto.WorkOrderDTO;
import com.mobile.service.dto.WorkOrderQueryDTO;
import com.mobile.service.inter.WorkOrderService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.web.config.util.Jurisdiction;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工单表API接口
 */
@RestController
@RequestMapping("/workOrder")
@Tag(name = "工单管理接口", description = "包含工单的查询、新增、修改、删除等操作")
public class WorkOrderController extends BaseController {

    @Autowired
    private WorkOrderService workOrderService;

    @PostMapping("/addWorkOrder")
    //@RequiresPermissions("workOrdermana:add:1")
    @Operation(summary = "创建工单")
    @AutoFill(OperateType.INSERT)
    public Object addWorkOrder(@Valid @RequestBody WorkOrderDTO dto) {
        Map<String, Object> map;
        try {
            map = workOrderService.addWorkOrder(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getWorkOrderListPage")
    //@RequiresPermissions("workOrdermana:read:1")
    @Operation(summary = "分页查询工单")
    public Object getWorkOrderListPage(@Validated WorkOrderQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<WorkOrderDTO> page = workOrderService.getWorkOrderListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put("QX", Jurisdiction.getHC(this.getUserCode(),"workOrder"));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getWorkOrderById")
    //@RequiresPermissions("workOrdermana:edit:1")
    @Operation(summary = "查询工单详情")
    public Object getWorkOrderById(
        @Parameter(description = "工单主键", required = true) 
        @RequestParam("workOrderId") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            WorkOrderDTO dto = workOrderService.getWorkOrderById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateWorkOrder")
    //@RequiresPermissions("WorkOrdermana:edit:1")
    @Operation(summary = "编辑工单")
    @AutoFill(OperateType.UPDATE)
    public Object updateWorkOrder(@Valid @RequestBody WorkOrderDTO dto) {
        Map<String, Object> map;
        try {
            map = workOrderService.updateWorkOrder(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeWorkOrder")
    //@RequiresPermissions("workOrdermana:delete:1")
    public Object removeWorkOrder(@RequestParam("workOrderId") Integer id) {
        Map<String, Object> map;
        try {
            map = workOrderService.removeWorkOrder(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getWorkOrderList")
    //@RequiresPermissions("workOrdermana:read:1")
    @Operation(summary = "条件查询工单列表")
    public Object getWorkOrderList(@Validated WorkOrderQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<WorkOrderDTO> list = workOrderService.getWorkOrderList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }


    @GetMapping(value = "/getWorkOrderStatistics")
    //@RequiresPermissions("workOrdermana:edit:1")
    @Operation(summary = "查询工单统计数据")
    public Object getWorkOrderStatistics() {
        Map<String, Object> map = new HashMap<>();
        try {
            WorkOrderDTO dto = workOrderService.getWorkOrderStatistics();
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
